package com.lin.manager.rabbitmq.enums;

import com.lin.manager.rabbitmq.ExchangeService;
import lombok.Getter;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.TopicExchange;

/**
 * @作者: 林江
 * @创建时间: 2024/4/14
 * @功能:
 */
@Getter
public enum RabbitEnum {
    DIRECT("direct", exchangeName -> new DirectExchange(exchangeName)),
    FANOUT("fanout", exchangeName -> new FanoutExchange(exchangeName)),
    TOPIC("topic", exchangeName -> new TopicExchange(exchangeName)),
    DELAY("delay", exchangeName -> new TopicExchange(exchangeName)),
    DEAD("dead", exchangeName -> new TopicExchange(exchangeName));

    private String exchangeType;
    private ExchangeService function;

    RabbitEnum(String exchangeType, ExchangeService function) {
        this.exchangeType = exchangeType;
        this.function = function;
    }

    public static Exchange getExchange(String type, String exchangeName) {
        for (RabbitEnum rabbitEnum : RabbitEnum.values()) {
            if (rabbitEnum.getExchangeType().equals(type)) {
                Exchange exchange = rabbitEnum.getFunction().create(exchangeName);
                return exchange;
            }
        }
        return null;
    }
}
